package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.EmDamage;
import com.ruoyi.system.domain.EmRecord;
import com.ruoyi.system.domain.EmWarning;
import com.ruoyi.system.mapper.EmRecordMapper;
import com.ruoyi.system.service.IEmDamageService;
import com.ruoyi.system.service.IEmRecordService;
import com.ruoyi.system.service.IEmWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 档案管理Service业务层处理
 *
 * @author yangcan
 * @date 2023-03-07
 */
@Service
public class EmRecordServiceImpl extends ServiceImpl<EmRecordMapper,EmRecord> implements IEmRecordService
{
    @Autowired
    private EmRecordMapper emRecordMapper;

    @Autowired
    private IEmDamageService emDamageService;

    @Autowired
    private IEmWarningService warningService ;

    /**
     * 查询档案管理
     *
     * @param id 档案管理主键
     * @return 档案管理
     */
    @Override
    public EmRecord selectEmRecordById(Long id)
    {
        return emRecordMapper.selectById(id);
    }

    /**
     * 查询档案管理列表
     *
     * @param emRecord 档案管理
     * @return 档案管理
     */
    @Override
    public List<EmRecord> selectEmRecordList(EmRecord emRecord)
    {
        return emRecordMapper.selectEmRecordList(emRecord);
    }

    /**
     * 新增档案管理
     *
     * @param emRecord 档案管理
     * @return 结果
     */
    @Override
    public int insertEmRecord(EmRecord emRecord)
    {
        emRecord.setCreateTime(DateUtils.getNowDate());

        if (!Objects.isNull(emRecord.getEmTypeId())){
            EmDamage damage = emDamageService.selectEmDamageById(emRecord.getEmTypeId());
            emRecord.setEmType(damage.getTypeName());
            emRecord.setEmName(damage.getName());
            emRecord.setHandleMethod(damage.getHandleMethod());
        }


        return emRecordMapper.insert(emRecord);
    }

    /**
     * 修改档案管理
     *
     * @param emRecord 档案管理
     * @return 结果
     */
    @Override
    public int updateEmRecord(EmRecord emRecord)
    {
        emRecord.setUpdateTime(DateUtils.getNowDate());
        if (!Objects.isNull(emRecord.getEmTypeId())){
            EmDamage damage = emDamageService.selectEmDamageById(emRecord.getEmTypeId());
            emRecord.setEmType(damage.getTypeName());
            emRecord.setEmName(damage.getName());
            emRecord.setHandleMethod(damage.getHandleMethod());

        }
        return emRecordMapper.updateById(emRecord);
    }

    /**
     * 批量删除档案管理
     *
     * @param ids 需要删除的档案管理主键
     * @return 结果
     */
    @Override
    public int deleteEmRecordByIds(Long[] ids)
    {
        return emRecordMapper.deleteEmRecordByIds(ids);
    }

    /**
     * 删除档案管理信息
     *
     * @param id 档案管理主键
     * @return 结果
     */
    @Override
    public int deleteEmRecordById(Long id)
    {
        return emRecordMapper.deleteEmRecordById(id);
    }

    @Override
    public void addData() {
        List<EmDamage> emDamages = emDamageService.selectEmDamageList(new EmDamage());
        for (int i = 0; i < 300; i++) {
            for (EmDamage emDamage : emDamages) {
                EmRecord emRecord = new EmRecord();
                emRecord.setEmName(emDamage.getName());
                emRecord.setEmTypeId(emDamage.getId());
                emRecord.setEmType(emDamage.getTypeName());
                emRecord.setContext(UUID.randomUUID().toString());
                emRecord.setTitle(UUID.randomUUID().toString());
                emRecord.setCreateTime(new Date());
                emRecordMapper.insertEmRecord(emRecord) ;
                EmWarning warning = new EmWarning();
                warning.setContext(emRecord.getContext());
                warning.setEmName(emRecord.getEmName());
                warning.setEmType(emRecord.getEmType());
                warning.setEmTypeId(emRecord.getEmTypeId());
                warning.setRecordId(emRecord.getId());
                warning.setTitle(emRecord.getTitle());
                warningService.insertEmWarning(warning) ;
            }
        }
    }
}
